﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using UTSDAL.LINQSQL.Model;
using UTSDAL.LINQSQL.DAO;


namespace UTS.Controls
{
    [
        DefaultProperty("Text"),
        ToolboxData("<{0}:RoomDropDownList runat=\"server\"> </{0}:RoomDropDownList>")
    ]
    public class RoomDropDownList : System.Web.UI.WebControls.DropDownList
    {
                
        private RoomDAO roomDAO = new RoomDAO();

        private void init()
        {

            this.DataSource = roomDAO.selectAll();
            this.DataBind();
            DropDownUtils.addDefaultLabel(this, "Select a Room");
            this.DataTextField = "RoomName";
            this.DataValueField = "RoomId";
        }

        // this method bypasses non post back requests by ajax
        public void reload()
        {
            init();
        }

        

        protected override void OnLoad(EventArgs e)
        {
                base.OnLoad(e);

                if (!Page.IsPostBack)
                {
                    init();
                    
                }
        }

        private Room selectedRoom;

        public Room SelectedRoom
        {
            get {
                selectedRoom = roomDAO.findById(new Guid(this.SelectedValue.ToString()));
                return selectedRoom; 
            }
            set 
            { 
                selectedRoom = value;
                if (selectedRoom != null)
                {
                    SelectedValue = selectedRoom.RoomId.ToString();
                }
            }
        }

    }
}